package com.bdqn.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.bdqn.entity.GroupModule;
import com.bdqn.entity.Module;

/**
 * A data access object (DAO) providing persistence and search support for
 * GroupModule entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bdqn.entity.GroupModule
 * @author MyEclipse Persistence Tools
 */

@SuppressWarnings("unchecked")
public class GroupModuleDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(GroupModuleDAO.class);

	public List<Module> findByGroupId(int id) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Module as m where m.moduleId in (select gm.module.moduleId from GroupModule as gm where gm.group.groupId=:ID )";
		Query query = session.createQuery(hql);
		query.setInteger("ID", id);
		return query.list();
	}
	
	public static void main(String[] args) {
		GroupModuleDAO dao = new GroupModuleDAO();
		List<Module> list = dao.findByGroupId(2);
		for (Module module : list) {
			System.out.println(module.getDisplayName());
		}
	}

	public void save(GroupModule transientInstance) {
		log.debug("saving GroupModule instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GroupModule persistentInstance) {
		log.debug("deleting GroupModule instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GroupModule findById(java.lang.Integer id) {
		log.debug("getting GroupModule instance with id: " + id);
		try {
			GroupModule instance = (GroupModule) getSession().get(
					"com.bdqn.entity.GroupModule", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GroupModule instance) {
		log.debug("finding GroupModule instance by example");
		try {
			List results = getSession().createCriteria(
					"com.bdqn.entity.GroupModule")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GroupModule instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GroupModule as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all GroupModule instances");
		try {
			String queryString = "from GroupModule";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GroupModule merge(GroupModule detachedInstance) {
		log.debug("merging GroupModule instance");
		try {
			GroupModule result = (GroupModule) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GroupModule instance) {
		log.debug("attaching dirty GroupModule instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GroupModule instance) {
		log.debug("attaching clean GroupModule instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}